package com.ad_hccm.controller;

import com.ad_hccm.domain.Appendix.AE;
import com.ad_hccm.domain.Appendix.SAE;
import com.ad_hccm.domain.PatientUser;
import com.ad_hccm.domain.User;
import com.ad_hccm.interceptor.TokenUtil;
import com.ad_hccm.service.Appendix.AEService;
import com.ad_hccm.service.Appendix.SAEService;
import com.ad_hccm.service.PatientUserService;
import com.ad_hccm.service.UserService;
import jakarta.servlet.http.HttpServletRequest;
import org.apache.el.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * TODO 搜索Controller
 */
@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private PatientUserService patientUserService;
    @Autowired
    private AEService aeService;
    @Autowired
    private SAEService saeService;

    @Autowired
    private UserService userService;

    @GetMapping("/patient_user/{search}")
    public Result searchUserList(@PathVariable String search, HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String,String> map = tokenUtil.parseToken(token);
        String userId = map.get("userId");
        User domain = userService.getByAccount(userId);
        search = '%'+search+'%';
        List<PatientUser> list = patientUserService.search(search, domain.getId());
        String msg = list != null ? "查询成功" : "没有匹配的数据";
        return new Result(Code.GET_OK, list, msg);
    }

    @GetMapping("/ae/{search}")
    public Result searchAE(@PathVariable String search, HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String,String> map = tokenUtil.parseToken(token);
        String userId = map.get("userId");
        System.out.println(userId);
        User domain = userService.getByAccount(userId);
        search = '%'+search+'%';
        List<AE> list = aeService.search(search, domain.getId());
        String msg = list != null ? "查询成功" : "没有匹配的数据";
        return new Result(Code.GET_OK, list, msg);
    }
    @GetMapping("/sae/{search}")
    public Result searchSAE(@PathVariable String search, HttpServletRequest request) {
        String token = request.getHeader("token");
        Map<String,String> map = tokenUtil.parseToken(token);
        String userId = map.get("userId");
        System.out.println(userId);
        User domain = userService.getByAccount(userId);
        search = '%'+search+'%';
        List<SAE> list = saeService.search(search, domain.getId());
        String msg = list != null ? "查询成功" : "没有匹配的数据";
        return new Result(Code.GET_OK, list, msg);
    }
}
